package com.bdqn.util;







import  org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
public class MybatisUtil {
    private static SqlSessionFactory sqlSessionFactory=null;
    private static SqlSession sqlSession=null;
    private static String resource= "mybatis-config.xml";
    static {
        //1.获取配置文件的输入流
        try {
            InputStream inputStream= Resources.getResourceAsStream(resource);
            sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //获取Sqlsession
    public static SqlSession getSqlSession(){
        //openSession()参数默认为false:取消自动提交事务,true:自动提交
        return sqlSessionFactory.openSession(false);
    }

    //关闭SqlSession
    public static void closeSQLSession(SqlSession sqlSession){
        if(sqlSession != null){
            sqlSession.close();
        }
    }

}
